
<template>
    <div>

        <avue-input-tree default-expand-all

                         :check-strictly="true"
                         v-bind:multiple="(column.multiple||column.multipleTest) ? 'multiple' : null"
                         :disabled="isDisabled"
                         v-model="currentValue"
                         placeholder="选择角色"
                         :dic="data"></avue-input-tree>
    </div>
</template>

<script>
    import {listCore as fetchRoleTree} from "@/framework-core/api/core/api";
    import { convertDic} from '@/framework-core/utils/util'
    export default {
      name: 'roleTreeChoose',
      props: ['column', 'modelValue'],
      inject: ['getViewFormData'],
      data() {
        return {
          currentValue: null,
          form: {},
          data: [],
          dataMap:{}
        }
      },
      computed: {
        isDisabled: function () {
          // if(!this.column){
          //     return true;
          // }
           return this.column.disabled;
          //return false;
        }
      },
      mounted() {
        if (this.getViewFormData) {
          this.formData = this.getViewFormData()
        }
      },

      created() {
        this.initData();
      },

      watch: {
        currentValue(newVal) {
          let  newValStr = newVal;
          let selectItems = []
          let flg = false;
          if(this.column.multiple||this.column.multipleTest){
            newValStr =newVal?newVal.join(","):[];
            newValStr.forEach(ele => {
              this.userDatas.push({id:ele,name:this.dataMap[ele]})
              if(this.dataMap[ele]){
                flg = true;
              }
            })
          }else{
            selectItems.push({id:newValStr,name:this.dataMap[newValStr]})
            if(this.dataMap[newValStr]){
              flg = true;
            }
          }
          this.$emit('update:modelValue', newValStr);
          if(flg){
            // 初始化时不要设置
            this.$emit('selectItemsObj', selectItems);
          }

        },
        modelValue:{
          immediate: true,   //
          deep: true,
          handler (newVal, oldVal) {
            if(this.column.multiple||this.column.multipleTest){
              this.currentValue = newVal?newVal.split(","):[];
            }else {
              this.currentValue = newVal;
            }

          }
        }
      },

      methods: {

        initData() {

          fetchRoleTree("sys/role", {}).then(response => {
            this.data = []
            const menu = {value: '', label: "请选择角色", children: []};
            convertDic(response.data.data.data, menu.children, true);
            response.data.data.data.forEach(node => {
              this.dataMap[node.id] = node.name;
            })
            this.data.push(menu);
          });
        }
      }

    };
</script>

<style >

</style>

<style lang="scss">

</style>
